const express = require('express')
const app = express()
const bodyParser = require('body-parser')
// ´¦Àí¾²Ì¬×ÊÔ´
app.use(express.static('public'))
// ´¦Àí²ÎÊý
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));

// ÉèÖÃÔÊÐí¿çÓò·ÃÎÊ¸Ã·þÎñ
app.all('*', function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header('Access-Control-Allow-Headers', ['Content-Type', 'mytoken']);
  next();
});


// async/await 处理多个异步任务！
app.get('/async1', (req, res) => {
    res.send('lvhang1')
})
app.get('/async2', (req, res) => {
  if (req.query.info === 'lvhang') {
    res.send('-lvchengxin')
  } else {
    res.send('luowenqi(error)')
  }
})



app.get('/adata', (req, res) => {
    res.send('BasedUsageOfAxios')
})


app.get('/axios', (req, res) => {
    res.send('axios get passing parameters ' + req.query.id)
})

app.delete('/axios', (req, res) => {
    res.send('axios delete passing parameters ' + req.query.id)
})


app.post('/axios', (req, res) => {
    res.send('axios post passing parameters ' + req.body.uname + '---' + req.body.pwd)
})

app.get('/axios/:id', (req, res) => {
    res.send('axios get(restful form) passing parameters ' + req.params.id)
})

app.put('/axios/:id', (req, res) => {
    res.send('axios put passing parameters ' + req.params.id + '---' + req.body.uname + '---' + req.body.pwd)
})


// axios 的响应结果与全局配置
app.get('/axios-json', (req, res) => {
    res.json({
      uname: 'lvhang',
      age: 23
    })
})




app.get('/fdata', (req, res) => {
  setTimeout(function() {
    res.send('BasedUsageOfFetch')
  },1000)
})

app.get('/books', (req, res) => {
  setTimeout(function() {
    res.send('Traditional URL passing parameters!' + req.query.id)
  },1000)
})

app.get('/books/:id', (req, res) => {
  setTimeout(function() {
    res.send('restful form URL passing parameters!' + req.params.id)
  },1000)
})

app.delete('/books/:id', (req, res) => {
  setTimeout(function() {
    res.send('Delete Request URL passing parameters!' + req.params.id)
  },1000)
})

app.post('/books', (req, res) => {
    res.send('post Request URL passing parameters!' + req.body.uname + '---' + req.body.age)
}) 

app.put('/books/:id', (req, res) => {
    res.send('put Request URL passing parameters!' + req.params.id + '---' + req.body.uname + '---' + req.body.age)
}) 

app.get('/json', (req, res) => {
    res.json({
      uname: 'lvhang',
      age: 23,
      gender: 'male'
    });
}) 



  




app.get('/a1', (req, res) => {
  setTimeout(function() {
    res.send('objectMethods1')
  },1000)
})
app.get('/a2', (req, res) => {
  setTimeout(function() {
    res.send('objectMethods2')
  },2000)
})
app.get('/a3', (req, res) => {
  setTimeout(function() {
    res.send('objectMethods3')
  },3000)
})








// Â·ÓÉ
app.get('/data', (req, res) => {
  res.send('Front-end interaction-Asynchronous programming and Promise overview-Ajax to achieve asynchronous programming! !')
})
app.get('/data1', (req, res) => {
  setTimeout(function() {
    res.send('¡¤The result sequence of multiple asynchronous calls is uncertain 1')
  },1000)
})
app.get('/data2', (req, res) => {
  res.send('¡¤The result sequence of multiple asynchronous calls is uncertain 2')
})

// Æô¶¯¼àÌý
app.listen(3000, () => {
  console.log('The background server is running! port: 3000...')
})